<template>
	<view class="header">
		<cu-custom bgColor="bg-gradual-blue" :isBack="false">
			<block slot="backText">返回</block>
			<block slot="content">萌宠乐园</block>
		</cu-custom>

		<view>
			<!-- 顶部卡片 -->
			<view style="position: relative;">
				<image src="https://p19.qhimg.com/bdr/740__/d/_open360/pet0403/11.jpg"
					style="height: 400rpx;width: 100%;"></image>
				<view style="position: absolute;top: 70%;text-align: center;width: 100%;">
					<uni-row>
						<uni-col :span="12">
							<view class="padding" style="height: 200rpx;width: 100%;width: 100%;">
								<view class="padding-xs floatBox" style="background-color: #ff557f;">
									<text class="text-white">最新消息</text>
								</view>
							</view>
						</uni-col>
						<uni-col :span="12">
							<view class="padding" style="height: 200rpx;width: 100%;" @click="addPetLose">
								<view class="padding-xs floatBox" style="background-color: #aaaaff;">
									<text class="text-white">发布寻宠</text>
									<view class="margin-top-xs" style="color: white;font-size: 25rpx;text-align: left;">
										丢失或者捡到宠物，你可以在这里发布
									</view>
								</view>
							</view>
						</uni-col>
					</uni-row>
				</view>
			</view>
			<!-- 占位 -->
			<view style="height: 80rpx;"></view>

			<!-- 查询 -->
			<view class="margin">
				<uni-row :gutter="10">
					<uni-col :span="6">
						<uni-row>
							<uni-col :span="4">
								<view style="height: 60rpx;line-height: 60rpx;text-align: center;">
									<uni-icons type="location-filled" color="#777" />
								</view>
							</uni-col>
							<uni-col :span="16">
								<uni-data-picker v-slot:default="{ data, error, options }" title="请选择所在地区"
									:localdata="ChinaCitysList" v-model="children" ref="picker" @change="selectCity">
									<view class="input-border">
										<view v-if="error" class="error">
											<text>{{ error }}</text>
										</view>
										<view v-else-if="data.length" class="input-selected"
											style="height: 60rpx;line-height: 60rpx;text-align: left;">
											<view v-for="(item, index) in data" :key="index" class="selected-item">
												<!-- <text>{{ petCity }}</text> -->
											</view>
											<text style="color: #6c6c6c;font-size: 22rpx;">{{ petCity }}</text>
										</view>
										<view v-else style="height: 60rpx;line-height: 60rpx;text-align: left;">
											<text style="color: #6c6c6c;font-size: 22rpx;">全国</text>
										</view>
									</view>
								</uni-data-picker>
							</uni-col>
							<uni-col :span="4">
								<view @click="clearCity" v-show="cityClear"
									style="height: 60rpx;line-height: 60rpx;text-align: right;">
									<uni-icons custom-prefix="iconfont" type="icon-clear" size="20" color="#bdbdbd" />
								</view>
							</uni-col>
						</uni-row>
					</uni-col>
					<uni-col :span="6">
						<view style="height: 60rpx;line-height: 60rpx;">
							<uni-data-select :border="false" placeholder="选择星球" :clear="true"
								v-model="queryParams.losePetStar" :localdata="petStarList" @change="selectPetSatr">
							</uni-data-select>
						</view>
					</uni-col>
					<uni-col :span="6">
						<view style="height: 60rpx;line-height: 60rpx;">
							<uni-data-select :border="false" placeholder="选择性别" :clear="true"
								v-model="queryParams.losePetSex" :localdata="sexList" @change="selectPetSex">
							</uni-data-select>
						</view>
					</uni-col>
					<uni-col :span="6">
						<uni-row>
							<uni-col :span="20">
								<view @click="open"
									style="color: #6c6c6c;font-size: 22rpx;height: 60rpx;line-height: 60rpx;">{{date}}
								</view>
							</uni-col>
							<uni-col :span="4">
								<view @click="clearDate" v-show="dateClear" style="height: 60rpx;line-height: 60rpx;">
									<uni-icons custom-prefix="iconfont" type="icon-clear" size="20" color="#bdbdbd" />
								</view>
							</uni-col>
						</uni-row>
					</uni-col>
				</uni-row>
			</view>
			<!--  -->
			<uni-calendar ref="calendar" class="uni-calendar--hook" :clear-date="false" :date="info.date"
				:insert="info.insert" :lunar="info.lunar" :startDate="info.startDate" :endDate="info.endDate"
				:range="info.range" @confirm="selectDate" />

			<!-- 宠物丢失 -->
			<view class="padding margin box" @click="handleLoseInfo(item.loseId)" v-for="(item,index) in losePetList"
				:key="item.loseId">
				<uni-row style="height: 250rpx;">
					<uni-col :span="10">
						<view>
							<image :src="baseUrl + item.loseImages.split(',')[0]"
								style="height: 250rpx;width: 250rpx;border-radius: 5%;margin-bottom: 0;"></image>
						</view>
					</uni-col>
					<uni-col :span="14">
						<view class="margin-left-sm" style="height: 50rpx;">
							<text>{{item.losePetNick}}，</text>
							<text>{{(item.losePetSex === "ZZ")?"男孩" :(item.losePetSex === "ZW")?"女孩":"未知"}}</text>
						</view>

						<view class="margin-left-sm" style="height: 100rpx;line-height: 100rpx;">
							<text style="font-size: 20rpx;">{{item.loseTitle}}</text>
						</view>

						<view class="margin-left-sm" style="height: 50rpx;">
							<uni-icons type="location-filled" color="#777" />
							<text style="color: cadetblue;font-size: 20rpx;">{{item.loseCity}}</text>
						</view>

						<view class="margin-left-sm" style="height: 50rpx;">
							<text style="font-size: 20rpx;">发布于：{{item.createTime}}</text>
						</view>

					</uni-col>
				</uni-row>
			</view>
			<!-- 底部占位 -->
			<view style="height: 100rpx;"></view>


		</view>

		<!-- 添加宠物星球 -->
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog ref="inputClose" mode="input" title="添加宠物星球" :value="petStarform.starName"
				placeholder="星球名称" @confirm="addPetSatr"></uni-popup-dialog>
		</uni-popup>

	</view>
</template>

<script>
	import ChinaCity from "../../static/data/ChinaCitysData.js"
	import {
		listStar,
		getStar,
		delStar,
		addStar,
		updateStar
	} from "@/api/pets/star";
	import {
		listLose
	} from '@/api/pets/lose.js'
	import {
		getUserProfile
	} from "@/api/system/user.js"
	import config from "@/config.js";

	export default {
		data() {
			return {
				baseUrl: config.baseUrl,
				//清除城市
				cityClear: false,
				//清除时间
				dateClear: false,
				//选择时间
				date: '选择时间',
				info: {
					lunar: true,
					range: true,
					insert: false,
					selected: []
				},
				children: "",
				ChinaCitysList: ChinaCity.data,
				//宠物所在的城市
				petCity: '',
				//宠物星球列表
				petStarList: [],
				//添加宠物星球
				petStarform: {
					starName: null,
					remark: null
				},
				//宠物性别列表
				sexList: [{
						value: 'ZZ',
						text: '男孩'
					},
					{
						value: 'ZW',
						text: '女孩'
					},
					{
						value: 'WW',
						text: '未知'
					}
				],
				//宠物丢失列表
				losePetList: [],
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 100,
					losePetNick: null,
					losePetSex: null,
					losePetStar: null,
					losePetType: null,
					loseCity: null,
					createBy: null,
					createTime: null,
				},
			}
		},
		created() {
			this.getAllPetStars();
			this.getAllLoseList();
		},
		methods: {
			//获取宠物遗失列表
			getAllLoseList() {
				listLose(this.queryParams).then(res => {
					this.losePetList = res.rows;
				})
			},
			//所有宠物星球
			getAllPetStars() {
				this.petStarList = [];
				listStar().then(res => {
					res.rows.forEach(item => {
						// uniapp中下拉框默认格式为data[{value:value,text:"text"}],这里需要转换一下接口数据
						this.petStarList.push({
							value: item.starId,
							text: item.starName
						})
					})
				})
			},
			//选择宠物星球
			selectPetSatr(e) {
				this.getAllLoseList();
			},

			//选择宠物性别
			selectPetSex(e) {
				this.getAllLoseList();
			},
			// 打开日历
			open() {
				this.$refs.calendar.open()
			},
			selectDate(e) {
				this.date = e.fulldate;
				this.dateClear = true;
				this.queryParams.createTime = e.fulldate;
				this.getAllLoseList();
			},
			//获取当前系统时间
			getDate(date) {
				date = new Date(date)
				const year = date.getFullYear()
				const month = date.getMonth() + 1
				const day = date.getDate()
				return `${year}-${this.addZero(month)}-${this.addZero(day)}`
			},
			//查看宠物丢失详细内容
			handleLoseInfo(loseId) {
				uni.navigateTo({
					url: '/pages/pet/lose/loseInfo?id=' + loseId
				})
			},
			//添加宠物丢失
			addPetLose() {
				uni.navigateTo({
					url: '/pages/pet/lose/addLose'
				})
			},
			//选择城市
			selectCity(e) {
				console.log(e)
				this.cityClear = true;
				this.petCity = e.detail.value[1].text;
				this.queryParams.loseCity = e.detail.value[0].text + '-' + e.detail.value[1].text + '-' + e.detail.value[2]
					.text;
				this.getAllLoseList();
			},
			//清空选中城市
			clearCity() {
				// this.petCity = "全国";
				this.$refs.picker.clear();
				this.cityClear = false;
				this.queryParams.loseCity = null;
				this.getAllLoseList();
			},
			//清空选中日期
			clearDate() {
				this.date = "选择时间";
				this.dateClear = false;
				this.queryParams.createTime = null;
				this.getAllLoseList();
			},
		}
	}
</script>

<style>
	.floatBox {
		/* border: 1px solid #b9b9b9; */
		border-radius: 20rpx;
		height: 150rpx;
		width: 100%;
	}

	.box {
		border-radius: 15rpx;
		border: 1px solid #b9b9b9;
		box-shadow: 1px 1px 5px 5px rgba(102, 102, 102, 0.2);
	}

	.header {
		/* //距离顶部 --status-bar-height 自动调整 */
		margin-top: var(--status-bar-height);
	}
</style>